//
//  RHPlantDeviceCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/4.
//

#import "RHPlantDeviceCell.h"
#import "RHDeviceModel.h"
#import "RHTools.h"

@interface RHPlantDeviceCell()
//有设备
@property (nonatomic ,strong) UIView *deviceView;
//没设备
@property (nonatomic ,strong) UIView *noDeviceView;

@property (nonatomic ,strong) UIImageView *markV;

@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,strong) QMUILabel *deviceNameLab;

@property (nonatomic ,strong) QMUILabel *deviceTitleLab;

@property (nonatomic ,strong) QMUILabel *deviceCountLab;

@property (nonatomic ,strong) QMUIButton *addBtn;

@property (nonatomic ,strong) QMUILabel *createTimeLab;

@property (nonatomic ,strong) QMUILabel *deviceTypeLab;

@property (nonatomic ,strong) QMUILabel *deviceSNLab;

@property (nonatomic ,strong) QMUILabel *deviceFirmwareLab;

@property (nonatomic, strong) NSArray *deviceListArr;

@property (nonatomic, assign) NSInteger totalCount;

@property (nonatomic, strong) MASConstraint *deviceBottom_constraint;

@property (nonatomic, strong) MASConstraint *noDeviceBottom_constraint;

@end
@implementation RHPlantDeviceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setDevices:(NSArray *)deviceArr totalCount:(NSInteger)count
{
    _deviceListArr = deviceArr;
    self.totalCount = count;
    if(count == 1){
        self.deviceView.hidden = NO;
        self.noDeviceView.hidden = YES;
        RHDeviceModel *deviceModel = [deviceArr firstObject];
        self.deviceNameLab.text = deviceModel.deviceName;
        self.createTimeLab.text = [NSDate dateStringWithTimeInterval:deviceModel.installTime/1000 formatter:@"yyyy-MM-dd HH:mm:ss"];
//        self.deviceTypeLab.text = [NSString stringWithFormat:@"%@-%@",[RHTools changeDevType:deviceModel.productTypeCode],deviceModel.deviceTypeCode];
        self.deviceTypeLab.text = [NSString stringWithFormat:@"%@-%@",deviceModel.productTypeCode,deviceModel.deviceTypeName];
        self.deviceSNLab.text = deviceModel.deviceId;
        self.deviceFirmwareLab.text = deviceModel.deviceSoftware;
        //在线状态
        if([deviceModel.status isEqualToString:@"1"]){//离线
            _markV.backgroundColor = kCOLOR_HEX(@"BBBBBB");
        }else if([deviceModel.status isEqualToString:@"0"]){//在线
            _markV.backgroundColor = CMainColorGreen;
        }else if([deviceModel.status isEqualToString:@"2"]){//故障
//            _markV.backgroundColor = kCOLOR_HEX(@"FA6334");
            _markV.backgroundColor = kCOLOR_HEX(@"BBBBBB");
        }
        
        //告警,故障状态
        if(deviceModel.normal == 2){
            //故障
            _markV.backgroundColor = kCOLOR_HEX(@"FA6334");
        }else if(deviceModel.normal == 1){
            _markV.backgroundColor = kRGB(251, 159, 41);
        }
        
        kWeakSelf;
        [self.noDeviceBottom_constraint uninstall];
        [self.deviceView mas_updateConstraints:^(MASConstraintMaker *make) {
            weakSelf.deviceBottom_constraint =  make.bottom.mas_offset(0).priorityHigh();
        }];
        
        
        
    }else{
        self.deviceView.hidden = YES;
        self.noDeviceView.hidden = NO;
        self.deviceCountLab.text = [NSString stringWithFormat:@"%ld%@",count,@"台".rh_localized];
        if(self.deviceListArr.count>1){
            self.addBtn.hidden = YES;
            self.deviceCountLab.hidden = NO;
            self.arrowImg.hidden = NO;
            self.deviceTitleLab.text = @"全部设备".rh_localized;
            [self.addBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(0);
                make.bottom.mas_offset(-5);
            }];
            
        }else{
            self.addBtn.hidden = NO;
            self.deviceCountLab.hidden = YES;
            self.arrowImg.hidden = YES;
            self.deviceTitleLab.text = @"暂无设备".rh_localized;
            [self.addBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(kScaleW(45));
                make.bottom.mas_offset(-15);
            }];
            //如果是安装工人以上角色,不允许添加设备
            if ([PrivateMange share].currentUser.hierarchyCode < UserRoleTypeTerminalUser) {
                self.addBtn.hidden = YES;
                [self.addBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(0);
                    make.bottom.mas_offset(0);
                }];
            }
        }
        kWeakSelf;
        [self.deviceBottom_constraint uninstall];
        [self.noDeviceView mas_updateConstraints:^(MASConstraintMaker *make) {
             weakSelf.noDeviceBottom_constraint = make.bottom.mas_offset(0).priorityHigh();
        }];
    }
    [self.deviceView setNeedsLayout];
    [self.noDeviceView setNeedsLayout];
    [self.deviceView layoutIfNeeded];
    [self.noDeviceView layoutIfNeeded];
}
- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
    }];
    
    if ([self.reuseIdentifier isEqualToString:@"NoDeviceCell"]) {
        [self addNoDeviceView:bgView];
    }
    
    if ([self.reuseIdentifier isEqualToString:@"OneDeviceCell"]) {
        [self addOneDeviceView:bgView];
    }
    
    if ([self.reuseIdentifier isEqualToString:@"MoreDeviceCell"]) {
        [self addMoreDeviceView:bgView];
    }
}

-(void)addOneDeviceView:(UIView*)bgView
{
    _markV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
    [_markV.layer setCornerRadius:4];
    [bgView addSubview:_markV];
    
    _deviceNameLab = [[QMUILabel alloc] init];
    [_deviceNameLab setTextAlignment:NSTextAlignmentLeft];
    [_deviceNameLab setFont:[UIFont systemFontOfSize:14]];
    [_deviceNameLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_deviceNameLab];
    
    _createTimeLab = [[QMUILabel alloc] init];
    [_createTimeLab setTextAlignment:NSTextAlignmentRight];
    [_createTimeLab setFont:[UIFont systemFontOfSize:11]];
    [_createTimeLab setTextColor:kCOLOR_HEX(@"AAAAAA")];
    [bgView addSubview:_createTimeLab];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];

    
    [_markV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(8, 8));
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    [_deviceNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.createTimeLab.mas_left).offset(-5);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.markV.mas_right).offset(5);
        make.centerY.equalTo(self.markV);
    }];

    [_createTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.deviceNameLab);
        make.right.mas_offset(-15);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.left.equalTo(self.markV);
        make.right.equalTo(self.createTimeLab);
        make.top.equalTo(self.markV.mas_bottom).offset(16);
    }];
    
    
    UIView *contentBg = [[UIView alloc] init];
    [contentBg setBackgroundColor:UIColor.clearColor];
    [bgView addSubview:contentBg];
    
    
    NSArray *titles = @[@"设备类型".rh_localized,@"SN编号".rh_localized,@"固件".rh_localized];
    NSMutableArray *items = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *lab = [[QMUILabel alloc] init];
        [lab setTextAlignment:NSTextAlignmentLeft];
        [lab setFont:[UIFont systemFontOfSize:13]];
        [lab setText:[NSString stringWithFormat:@"%@:",obj]];
        [lab setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.6]];
        [contentBg addSubview:lab];
        [items addObject:lab];
        
        QMUILabel *contentLab = [[QMUILabel alloc] init];
        [contentLab setTextAlignment:NSTextAlignmentRight];
        [contentLab setFont:[UIFont systemFontOfSize:13]];
        [contentLab setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:1]];
        [contentLab setText:@"--"];
        [contentBg addSubview:contentLab];
//            [items addObject:contentLab];
        
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(10);
            make.height.offset(30);
        }];
        
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(0);
            make.height.mas_equalTo(20);
            make.centerY.equalTo(lab);
            make.width.mas_equalTo(200);
        }];
        if(idx == 0){
            self.deviceTypeLab = contentLab;
        }else if (idx == 1){
            self.deviceSNLab = contentLab;
        }else if (idx == 2){
            self.deviceFirmwareLab = contentLab;
        }
    }];
    [contentBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lineV);
        make.top.equalTo(lineV.mas_bottom).offset(10);
        make.bottom.mas_offset(-10);
        
    }];
    
    [items mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(0);
       
    }];
    
    [items mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [_markV setBackgroundColor:CFMainColor];
    [_deviceNameLab setText:@"Device A"];
    
}

-(void)addMoreDeviceView:(UIView*)bgView
{
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"我的设备".rh_localized;
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.font = kBOLDSYSTEMFONT(16);
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    UIImageView *arrowIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kwt_home_arrow"]];
    [bgView addSubview:arrowIcon];
    [arrowIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kScaleW(18));
        make.right.mas_offset(-15);
        make.centerY.equalTo(titleLab);
    }];
    
    QMUIButton *deviceListBtn = [[QMUIButton alloc]init];
    [bgView addSubview:deviceListBtn];
    [deviceListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.right.equalTo(arrowIcon);
        make.centerY.equalTo(titleLab);
        make.height.offset(40);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.16);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    
    UIView *allDevBgView = kView;
    allDevBgView.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    allDevBgView.layer.cornerRadius  = 8;
    allDevBgView.layer.masksToBounds = YES;
    [bgView addSubview:allDevBgView];
    [allDevBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(line.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(-15);
    }];
    
    
    UIImageView *deviceIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kwt_plant_device"]];
    [allDevBgView addSubview:deviceIcon];
    [deviceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kScaleW(40));
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.bottom.mas_offset(-15);
    }];

    
    _deviceTitleLab = [[QMUILabel alloc] init];
    _deviceTitleLab.text = @"全部设备".rh_localized;
    [_deviceTitleLab setTextAlignment:NSTextAlignmentLeft];
    [_deviceTitleLab setFont:[UIFont systemFontOfSize:15]];
    [_deviceTitleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [allDevBgView addSubview:_deviceTitleLab];
    [_deviceTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(deviceIcon.mas_right).offset(5);
        make.centerY.equalTo(deviceIcon);
    }];
    
    _deviceCountLab = [[QMUILabel alloc] init];
    [_deviceCountLab setTextAlignment:NSTextAlignmentRight];
    [_deviceCountLab setFont:[UIFont systemFontOfSize:14]];
    [_deviceCountLab setTextColor:kCOLOR_HEX(@"AAAAAA")];
    [allDevBgView addSubview:_deviceCountLab];
    [_deviceCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.deviceTitleLab);
        make.right.equalTo(arrowIcon.mas_left);
    }];
    
    kWeakSelf;
    [[deviceListBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.deviceListBlock){
            weakSelf.deviceListBlock();
        }
    }];
    
}

-(void)addNoDeviceView:(UIView*)bgView
{
    QMUILabel *titleLab = [QMUILabel labelWithText:@"我的设备".rh_localized font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#171A1D")];
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    QMUILabel *noDevLab = [QMUILabel labelWithText:@"暂无设备".rh_localized font:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.4)];
    [bgView addSubview:noDevLab];
    [noDevLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(titleLab);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.16);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    QMUIButton *addBtn = [[QMUIButton alloc]init];
    [addBtn setTitle:@"添加设备".rh_localized forState:UIControlStateNormal];
    [addBtn setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
    addBtn.titleLabel.font = kSYSTEMFONT(15);
    [addBtn setImage:[UIImage imageNamed:@"kwt_add_device"] forState:UIControlStateNormal];
    addBtn.imagePosition = QMUIButtonImagePositionLeft;
    addBtn.spacingBetweenImageAndTitle = 10;
    addBtn.backgroundColor = kCOLOR_HEX(@"F6F8FA");
    addBtn.cornerRadius = 5;
    [bgView addSubview:addBtn];
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(15);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(kScaleW(45));
        make.bottom.mas_offset(-18);
    }];
    
    kWeakSelf;
    [[addBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.addDeviceBlock){
            weakSelf.addDeviceBlock();
        }
    }];
    
}
@end
